package com.wl.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wl.dao.MaterialMapper;
import com.wl.entity.Material;
import com.wl.service.MaterialService;
import com.wl.util.PageData;

/**
 * 业务层实现类
 * 
 * @author HCH
 *
 */
@Service
public class MaterialServiceImpl implements MaterialService {

	// 注入一个MaterialMapper
	@Autowired
	private MaterialMapper materialMapper;

	/**
	 * 添加物料信息
	 */
	@Override
	public boolean insert(Material record) {
		// TODO Auto-generated method stub
		int n = materialMapper.insert(record);
		if (n > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据id删除物料信息
	 */
	@Override
	public boolean deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		int n = materialMapper.deleteByPrimaryKey(id);
		if (n > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 分页模糊查询
	 */
	@Override
	public PageData<Material> selectMaterialByLike(int page, int limit, String keywords, String materialid,
			Double minprice, Double maxprice,Integer materialstatus) {
		// TODO Auto-generated method stub
		// 设置起始位置
		int start = 0;
		if (page >= 1) {
			start = (page - 1) * limit;
		}
		// 查询总页数
		int totalCount = materialMapper.selectMaterialByLikeCount(keywords, materialid, minprice, maxprice,materialstatus);
		// 分页的数据
		List<Material> data = materialMapper.selectMaterialByLike(start, limit, keywords, materialid, minprice,
				maxprice,materialstatus);
		// 构造方法：赋值内容为 数据、总记录数、limit、page
		PageData<Material> pageData = new PageData<Material>(data, totalCount, limit, page);
		// code、Msg，前端layui所需要的参数
		pageData.setCode(0);
		pageData.setMsg("");

		return pageData;
	}

	/**
	 * 根据id动态修改物料信息
	 */
	@Override
	public boolean updateByPrimaryKeySelective(Material record) {
		// TODO Auto-generated method stub
		int n = materialMapper.updateByPrimaryKeySelective(record);
		if (n > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据id查找物料
	 */
	@Override
	public Material selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		Material material = materialMapper.selectByPrimaryKey(id);
		return material;
	}

	/**
	 * 根据物料编号查找物料
	 */
	@Override
	public Material selectByMaterialId(String materialid) {
		// TODO Auto-generated method stub
		Material material = materialMapper.selectByMaterialId(materialid);
		return material;
	}

}
